﻿Imports System.Transactions
Imports UtilPadrao
Public Class ctrUsuario
    Inherits ControlePadrao.ctrPadrao

#Region "Variáveis"

    Private objUsuario As New Persistencia.perUsuario

    Public sCamposDemonstracao As String() = {"Identificador", "Nome", "Login"}
    Public sCamposTabela As String() = {"codUsuario", "nome", "login"}
    Public sCamposChave As String() = {"codUsuario"}
    Public sTitulosColunas As String() = {"Identificador", "Nome", "Login"}

    Public Mensagens As String() = {"Insira o nome.",
                                    "Insira o login.",
                                    "Insira a senha.",
                                    "Login já cadastrado."}

#End Region

#Region "Propriedades"
    Public Property codigoUsuario As Int32
#End Region

#Region "Enumerações"

    Public Enum eMensagens
        NOME = 0
        LOGIN = 1
        SENHA = 2
        CADASTRADO = 3
    End Enum

#End Region

#Region "Métodos"

    Public Function gravarUsuario(ByVal iCodUsuario As Integer,
                                  ByVal sNome As String,
                                  ByVal sLogin As String,
                                  ByVal sSenha As String,
                                  ByVal bBloqueado As Boolean,
                                  ByVal datGrupos As DataTable,
                                  ByVal datEmpresas As DataTable) As Boolean

        Dim bAbrirFecharConexao As Boolean = False
        Dim bRetorno As Boolean = False

        Try

            Me.limparMensagensValidacao()

            If Me.validarUsuario(eTipoValidacao.GRAVACAO,
                                 iCodUsuario,
                                 sNome,
                                 sLogin,
                                 sSenha) Then

                Using scope As New TransactionScope

                    Me.objUsuario.configurarFechamentoConexao()

                    If iCodUsuario = 0 Then

                        Me.codigoUsuario = Me.objUsuario.inserirUsuario(sNome,
                                                                        sLogin,
                                                                        sSenha,
                                                                        bBloqueado,
                                                                        bAbrirFecharConexao)

                    Else

                        Me.codigoUsuario = iCodUsuario

                        Me.objUsuario.editarUsuario(Me.codigoUsuario,
                                                    sNome,
                                                    sLogin,
                                                    sSenha,
                                                    bBloqueado,
                                                    bAbrirFecharConexao)

                    End If

                    Me.objUsuario.excluirUsuarioEmpresa(Me.codigoUsuario,
                                                        bAbrirFecharConexao)

                    Me.objUsuario.excluirUsuarioGrupo(Me.codigoUsuario,
                                                      bAbrirFecharConexao)

                    For Each drDados As DataRow In datGrupos.Rows

                        If conversao.toBoolean(drDados.Item("permitido")) Then

                            Me.objUsuario.inserirUsuarioGrupo(Me.codigoUsuario,
                                                              conversao.toInt32(drDados.Item("codGrupo")),
                                                              conversao.toInt32(drDados.Item("codEmpresa")),
                                                              bAbrirFecharConexao)

                        End If

                    Next

                    For Each drEmpresa As DataRow In datEmpresas.Rows

                        If conversao.toBoolean(drEmpresa.Item("permitido")) Then

                            Me.objUsuario.inserirUsuarioEmpresa(Me.codigoUsuario,
                                                                conversao.toInt32(drEmpresa.Item("codEmpresa")),
                                                                bAbrirFecharConexao)

                        End If

                    Next

                    scope.Complete()

                    Me.objUsuario.configurarFechamentoConexao()

                    bRetorno = True

                End Using

            End If

            Return bRetorno

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    Public Function excluirUsuario(ByVal iCodUsuario As Integer) As Boolean

        Dim bResultado As Boolean = True

        Try

            Using scope As New TransactionScope

                Me.objUsuario.configurarFechamentoConexao()

                Me.objUsuario.excluirUsuarioGrupo(iCodUsuario, False)
                Me.objUsuario.excluirUsuario(iCodUsuario, False)

                Me.objUsuario.configurarFechamentoConexao()

                scope.Complete()

            End Using

            Return bResultado

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    Public Function selecionarUsuarios() As DataTable
        Try
            Return Me.objUsuario.selecionarUsuarios
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

    Public Function selecionarUsuarioCodigo(ByVal iCodigoUsu As Integer) As DataRow
        Try
            Return Me.objUsuario.selecionarUsuarioCodigo(iCodigoUsu)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

    Public Function selecionarUsuariosGrupos(ByVal iCodigoUsu As Integer) As DataTable
        Try
            Return Me.objUsuario.selecionarUsuariosGrupos(iCodigoUsu)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

    Public Function selecionarModulosGrupos() As DataTable
        Try
            Return Me.objUsuario.selecionarModulosGrupos
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

    Public Function seUsuarioExistente(ByVal sLogin As String,
                                       ByVal iCodUsu As Int32) As Boolean

        Try
            Return Me.objUsuario.seUsuarioExistente(sLogin, iCodUsu)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    Public Function selecionarUsuario(ByVal sLogin As String,
                                  ByVal sSenha As String) As DataRow

        Try
            Return Me.objUsuario.selecionarUsuario(sLogin, sSenha)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    Private Function validarUsuario(ByVal tipoValidacao As eTipoValidacao,
                                    ByVal iCodUsuario As Int32,
                                    ByVal sNome As String,
                                    ByVal sLogin As String,
                                    ByVal sSenha As String) As Boolean

        Dim bRetorno As Boolean = True

        Try

            Select Case tipoValidacao

                Case eTipoValidacao.GRAVACAO

                    If String.IsNullOrWhiteSpace(sNome) Then
                        MyBase.adicionarMensagensValidacao(eMensagens.NOME,
                                                           ctrEmpresa.eTipoLog.ERRO,
                                                           Me.Mensagens(eMensagens.NOME))
                        bRetorno = False

                    End If

                    If String.IsNullOrWhiteSpace(sLogin) Then
                        MyBase.adicionarMensagensValidacao(eMensagens.LOGIN,
                                                           ctrEmpresa.eTipoLog.ERRO,
                                                           Me.Mensagens(eMensagens.LOGIN))
                        bRetorno = False
                    End If

                    If String.IsNullOrWhiteSpace(sSenha) Then
                        MyBase.adicionarMensagensValidacao(eMensagens.SENHA,
                                                           ctrEmpresa.eTipoLog.ERRO,
                                                           Me.Mensagens(eMensagens.SENHA))
                        bRetorno = False
                    End If

                    If Me.objUsuario.seUsuarioExistente(sLogin, iCodUsuario) Then
                        MyBase.adicionarMensagensValidacao(eMensagens.CADASTRADO,
                                                           ctrEmpresa.eTipoLog.ERRO,
                                                           Me.Mensagens(eMensagens.CADASTRADO))
                        bRetorno = False
                    End If

                Case eTipoValidacao.EXCLUSAO



            End Select

        Catch ex As Exception
            Throw New Exception(Environment.NewLine)
        End Try

        Return bRetorno

    End Function

    Public Function selecionarUsuarioEmpresas(ByVal iCodUsuario As Int32) As DataTable
        Try
            Return Me.objUsuario.selecionarUsuarioEmpresas(iCodUsuario)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function
#End Region

End Class
